package com.example.back.controller;

import com.example.back.common.GetNow;
import com.example.back.entity.Post;
import com.example.back.entity.User;
import com.example.back.entity.otd.Posts;
import com.example.back.entity.otd.QueryPageDto;
import com.example.back.mapper.otd.PostsMapper;
import com.example.back.service.PostService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private GetNow getNow;
    @PostMapping("/post")
    public int addPost(@RequestBody Post post){
        post.setBrowse(0);
        System.out.println( post);
       String time = getNow.getNow();
       post.setTime(time);
        return postService.addPost(post);
    }
    @DeleteMapping("/post/{id}")
    public int deletePost(@PathVariable("id") Integer id){
        return postService.deletePost(id);
    }
    @PutMapping("/post")
    public int updatePost(@RequestBody Post post){
        return postService.updatePost(post);
    }
    @GetMapping("/poster/{id}")
    public Post queryPostById(@PathVariable("id") Integer id){
        postService.addBrowse(id);
        return postService.queryPostById(id);
    }
    @GetMapping("/post")
    public ArrayList<Post> queryPost( Post post){
        return postService.queryPost(post);
    }
    @GetMapping("/top")
    public ArrayList<Post> getTopPostsByBrowse(){
        return postService.getTopPostsByBrowse();
    }
    @GetMapping("/postUser")
    public ArrayList<Posts> getPostsAndUser(@RequestParam(required = false) String text, @RequestParam(required = false) String time){
        return postService.getPostsAndUser(text, time);
    }
@GetMapping("/postUser/ui")
public PageInfo<Posts> doList(@RequestParam(required = false) String text,
                              @RequestParam(required = false) String time,
                              @RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "8") Integer pageSize) {
    // 分页
    PageHelper.startPage(pageNum, pageSize);
    // 查询
    ArrayList<Posts> list = postService.getPostsAndUser(text, time);
    // 返回分页信息Posts
    return new PageInfo<Posts>(list);
}

    @GetMapping("/post/analysis1")  // 前端将请求这个接口
    public Map<String, Object> getPostAnalysis() {
        // 1. 获取所有帖子的发布时间列表
        List<String> allPostTimes = postService.getAllPostTimes();
        // 2. 构建返回数据（前端需要的`rawTimes`字段）
        Map<String, Object> result = new HashMap<>();
        result.put("rawTimes", allPostTimes);  // 键名必须是`rawTimes`，与前端对应

        return result;
    }
    @GetMapping("/postUser/YW/{userId}")
    public ArrayList<Post>queryPostByUserId(@PathVariable("userId") String userId)
    {
        return postService.queryPostByUserId(Integer.parseInt(userId));
    }
}
